using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Project.WebSocketService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging;
using Project.SemanticKernelService;
using Project.SemanticKernelService.Model;

namespace Project.Admin.Controllers
{
    /// <summary>
    /// 测试
    /// </summary>
    [ApiController]
    [Area("api")]
    [Route("api/[controller]/[action]")]
    public class WebSocketChatController : Controller
    {
        private readonly ISocketService _socketService;
        private readonly ILogger<WebSocketChatController> _logger;
        public static Dictionary<string, WebSocket> _userConnections = new Dictionary<string, WebSocket>();

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="openAIService"></param>
        public WebSocketChatController(ILogger<WebSocketChatController> logger, ISocketService socketService)
        {
            _logger = logger;
            _socketService = socketService;
        }

        [HttpGet]
        public async Task GetWebSocketReceive()
        {
            if (HttpContext.WebSockets.IsWebSocketRequest)
            {
                try
                {
                    var socket = await HttpContext.WebSockets.AcceptWebSocketAsync();
                    await _socketService.WebSocketReceive(socket);
                }
                catch (Exception ex)
                {
                    _logger.Equals(ex.Message);
                }
            }
            else
            {
                HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
            }
        }
    }
}
